import { createRouter, createWebHistory } from "vue-router";
// 在指定私有路由表的时候，会大量地使用到/src/layout/index.vue，所以这里就不进行懒加载了。
import layout from "@/layout/index.vue";
import ArticleCreaterRouter from './modules/ArticleCreate'
import ArticleRouter from './modules/Article'
import PermissionListRouter from './modules/PermissionList'
import RoleListRouter from './modules/RoleList'
import UserManageRouter from './modules/UserManage'
import store from '@/store'
/**
 * 私有路由表
 */
export const  privateRoutes = [
  RoleListRouter,
  UserManageRouter,
  PermissionListRouter,
  ArticleCreaterRouter,
  ArticleRouter
]
// console.log(privateRoutes)
/**
 * 公开路由表
 */
export const  publicRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index")
  },
  {
    path: "/",
    // 注意：带有路径“/”的记录中的组件“默认”是一个不返回 Promise 的函数
    component: layout,
    // 重定向到个人中心页面
    redirect: "/profile",
    children: [
      {
        // 个人中心
        path: "/profile",
        name: "profile",
        component: () => import("@/views/profile/index"),
        meta: {
          title: "profile",
          icon: "user"
        }
      },
      {
        path: "/chart",
        name: "chart",
        component: () => import(/* webpackChunkName: "chart" */ '@/views/chart/index'),
        meta: {
          title: "chart",
          icon: "chart"
        }
      },
      {
        path: "/401",
        name: "401",
        component: () => import("@/views/error-page/401")
      }
    ]
  }
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes: publicRoutes
});
/**
 * 初始化路由表
 */
export function resetRouter() {
  if (
      store.getters.userInfo &&
      store.getters.userInfo.permission &&
      store.getters.userInfo.permission.menus
  ) {
    const menus = store.getters.userInfo.permission.menus
    menus.forEach((menu) => {
      router.removeRoute(menu)
    })
  }
}
export default router;
